package com.chen10.cattle.ai.inter.impl;

import com.chen10.cattle.ai.inter.GobangAlgorithm;
import com.chen10.cattle.ai.model.AiMoveRequest;
import com.chen10.cattle.ai.model.Position;

public class EasyGobangAlgorithm implements GobangAlgorithm {
    @Override
    public Position calculateMove(AiMoveRequest aiMoveRequest) {
        int[] bestMove = getNextMove(aiMoveRequest.getBoard(), aiMoveRequest.getAiColor());
        return new Position(bestMove[0], bestMove[1]);
    }

    public int[] getNextMove(int[][] board, int aiColor) {
        int size = board.length;
        int bestScore = Integer.MIN_VALUE;
        int[] bestMove = new int[]{-1, -1};
        int opponent = -aiColor;
        // 定义方向：水平、垂直、两个对角线
        int[][] directions = {
                {0, 1}, {1, 0}, {1, 1}, {1, -1}
        };

        // 遍历棋盘的每一个位置
        for (int row = 0; row < size; row++) {
            for (int col = 0; col < size; col++) {
                if (board[row][col] != 0) continue; // 如果不是空位，跳过

                int score = 0;

                // 检查所有方向
                for (int[] dir : directions) {
                    int countAI = 0;
                    int countOpponent = 0;

                    // 向一个方向遍历
                    for (int i = 1; i < 5; i++) {
                        int r = row + dir[0] * i;
                        int c = col + dir[1] * i;
                        if (r < 0 || r >= size || c < 0 || c >= size) break;
                        if (board[r][c] == aiColor) countAI++;
                        else if (board[r][c] == opponent) {
                            countOpponent++;
                            break;
                        } else break;
                    }

                    // 向反方向遍历
                    for (int i = 1; i < 5; i++) {
                        int r = row - dir[0] * i;
                        int c = col - dir[1] * i;
                        if (r < 0 || r >= size || c < 0 || c >= size) break;
                        if (board[r][c] == aiColor) countAI++;
                        else if (board[r][c] == opponent) {
                            countOpponent++;
                            break;
                        } else break;
                    }

                    // 简单评分机制
                    if (countAI > 0 && countOpponent == 0) {
                        score += Math.pow(10, countAI);
                    }
                    if (countOpponent > 0 && countAI == 0) {
                        score += Math.pow(10, countOpponent) * 1.5;
                    }
                }

                // 更新最佳得分和位置
                if (score > bestScore) {
                    bestScore = score;
                    bestMove[0] = row;
                    bestMove[1] = col;
                }
            }
        }

        // 如果没有找到合适的移动，选择第一个空位
        if (bestMove[0] == -1 && bestMove[1] == -1) {
            for (int row = 0; row < size; row++) {
                for (int col = 0; col < size; col++) {
                    if (board[row][col] == 0) {
                        return new int[]{row, col};
                    }
                }
            }
        }

        return bestMove;
    }

}
